﻿using Propro.Enum;
using Propro.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Propro.LogWriter
{
    public class LoggerWriter
    {

        private static ProWebContext _db = new ProWebContext();

        public static void log(LogSoort soort, String message, String user = "")
        {

            System.IO.StreamWriter writer = System.IO.File.AppendText(@"C:\log\propro.log");
            if (user != "")
            {
                writer.WriteLine(soort.ToString() + " at : " + DateTime.Now + " : " + message + " : " + user);
            }
            else
            {
                writer.WriteLine(soort.ToString() + " at : " + DateTime.Now + " : " + message);
            }
            writer.Close();

            Log log = new Log();
            log.soort = soort;
            if (user != "")
            {
                log.User = _db.Users.Where(m => m.UserName == user).First();
            }
            log.info = message;


            try
            {
                _db.Logs.Add(log);
                _db.SaveChanges();
            }
            catch (Exception ex)
            {
                
                writer.WriteLine(LogSoort.error + " at : " + DateTime.Now + " : " + " ERROR WRITING LOG TO DATABASE " + ex.Message);
            }
            
        }
    }
}